package org.smile.collection;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

public class JoinEnumeration<E> implements Enumeration<E> {
    Set<E> set;
    Iterator<E> iterator;
    Enumeration<E> enumeration;
    E next = null;

    public JoinEnumeration(Set<E> first, Enumeration<E> join) {
        this.set = first;
        this.iterator = first.iterator();
        this.enumeration = join;
    }

    public boolean hasMoreElements() {
        if (this.next == null) {
            if (this.iterator.hasNext()) {
                this.next =  this.iterator.next();
            } else if (this.enumeration != null) {
                while (this.next == null && this.enumeration.hasMoreElements()) {
                    this.next =  this.enumeration.nextElement();
                    if (this.set.contains(this.next)) {
                        this.next = null;
                    }
                }
            }
        }
        return this.next != null;
    }

    public E nextElement() {
        if (this.hasMoreElements()) {
            E var1 = this.next;
            this.next = null;
            return var1;
        } else {
            throw new NoSuchElementException();
        }
    }
}
